package bg.cinema_system.DAO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

public class UserInfoDAO {

	private long user_id;
	private DataSource dataSource;

	public UserInfoDAO(long movie_id, DataSource dataSource) {
		this.user_id = movie_id;
		this.dataSource = dataSource;
	}

	// Connect to DB and get the username
	public String getUsername() throws SQLException {
		// Check if there is such DataSource
		if (dataSource == null) {
			throw new SQLException("Class UserInfoDAO: Can't get data source");
		}

		// Get database connection
		Connection dbConnection = dataSource.getConnection();

		if (dbConnection == null) {
			throw new SQLException(
					"Class UserInfoDAO: Can't get database connection");
		}

		String sqlStatement = "SELECT users.username FROM users WHERE users.user_id ='"
				+ user_id + "'";

		PreparedStatement ps = dbConnection.prepareStatement(sqlStatement);

		// Get customer data from database
		ResultSet result = ps.executeQuery();

		String username = "";

		while (result.next()) {
			username = result.getString("username");
		}

		return username;
	}
}
